package org.ysu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.minio.Result;
import io.minio.errors.*;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.ysu.entity.Admin;
import org.ysu.service.AdminRoleService;
import org.ysu.service.AdminService;
import org.ysu.util.JwtUtil;
import org.ysu.vo.CenterVO;
import org.ysu.vo.LoginVO;
import org.ysu.vo.ResultVO;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author LiuShuo
 * @since 2024-08-14
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    AdminService adminService;

    @Resource
    AdminRoleService adminRoleService;

    @GetMapping("/list")
    ResultVO<IPage<Admin>> list(Integer pageNo, Integer pageSize, String val,Long openId ) throws InterruptedException {
        System.out.println(openId);
        return ResultVO.success(adminService.list(pageNo, pageSize, val));
    }

    @PostMapping("/add")
    ResultVO<Boolean> add(String name, Integer sex, String phone, String idcard, String email, String password,  Long areaId, MultipartFile file) throws InterruptedException, ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVO.success(adminService.add(name, sex, phone, idcard, email,  password,  areaId,file), !idcard.isEmpty() ? "添加管理员成功" : "注册成功");
    }

    @GetMapping("/check")
    ResultVO<Boolean> check(Long id, String field, String value) {
        return ResultVO.success(adminService.check(id,field,value));
    }
    @GetMapping("/getById")
    ResultVO<Admin> getById(Long id){
        return ResultVO.success(adminService.getOne(id));
    }
    @PostMapping("/update")
    ResultVO<Boolean> update(Long id, String name, Long areaId, String phone, Integer sex, String idcard, String email, MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVO.success(adminService.update(id,name,areaId,phone,sex,idcard,email,file),"修改成功");
    }
    @PostMapping("/del")
    ResultVO<Boolean> del(Long id, Boolean status) {
        return ResultVO.success(adminService.del(id, status), status ? "启用管理员成功" : "禁用管理员成功");
    }
    @GetMapping("/getNameByAdminId")
    ResultVO<List<String>> getNameByAdminId(Long adminId){
        return ResultVO.success(adminService.getNameByAdminId(adminId));
    }
    @PostMapping("/login")
    ResultVO<LoginVO> login(String username, String password) {
        return ResultVO.success(adminService.login(username,password));
    }
    @PostMapping("/sendCode")
    ResultVO<Boolean> sendCode(String email) throws MessagingException {
        return ResultVO.success(adminService.sendCode(email)) ;
    }
    @PostMapping("/checkCode")
    ResultVO<Boolean> checkCode(String email,String code){
        return ResultVO.success(adminService.checkCode(email,code));
    }
    @PostMapping("/register")
    ResultVO<Boolean> register(String name, Integer sex, String idcard, String phone, String email, String password, Long areaId) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVO.success(adminService.register(name, sex, idcard, phone, email, password, areaId));
    }
    @PostMapping("/change")
    ResultVO<Boolean> change(String email,String password){
        return ResultVO.success(adminService.change(email,password));
    }
    @GetMapping("/center")
    ResultVO<CenterVO> center(Long id){
        return ResultVO.success(new CenterVO(adminRoleService.getRoleId(id),adminService.getOne(id)));
    }
}
